﻿using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.JSON;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Util;
using System.ComponentModel;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;

namespace RCE.K3.YGJD.Business.PlugIn.SAL
{
    [HotUpdate]
    [Description("发货通知单 销售数量小于库存数量")]
    public class DeliveryNoticeBusinessPlugIn : AbstractBillPlugIn
    {

        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            var FBillNo = this.View.Model.GetValue("FBillNo");
            if (FBillNo != null && FBillNo.ToString() != "")
                setEntryColor();
        }

        public override void AfterSave(AfterSaveEventArgs e)
        {
            base.AfterSave(e);
            setEntryColor();
        }


        public void setEntryColor()
        {
            var entityKey = "FEntity";
            var rowCount = this.View.Model.GetEntryRowCount(entityKey);
            var backColor = "#FF0000";
            for (int i = 0; i < rowCount; i++)
            {

                var materialIdObj = this.View.Model.GetValue("FMaterialId", i) as DynamicObject;
                string materialId = "0";
                if (materialIdObj != null)
                    materialId = materialIdObj["Id"].ToString();

                var stockObj = this.View.Model.GetValue("FStockId", i) as DynamicObject;
                string stockId = "0";
                if (stockObj != null)
                    stockId = stockObj["Id"].ToString();

                decimal qty = Convert.ToDecimal(this.View.Model.GetValue("FQty", i));

                string sql = $"select isnull(sum(FBASEQTY),0) FBASEQTY from T_STK_INVENTORY where FMATERIALID = '{materialId}' and FSTOCKID = '{stockId}' ";

                var dataRows = DBUtils.ExecuteDynamicObject(this.Context, sql);

                if (dataRows.Count <= 0)
                {
                    GridSetRowBackcolor(this.View, entityKey, backColor, i);
                    continue;
                }
                decimal invQty = Convert.ToDecimal(dataRows[0]["FBASEQTY"]);
                if (invQty < qty)
                {
                    GridSetRowBackcolor(this.View, entityKey, backColor, i);
                    continue;
                }
                GridSetRowBackcolor(this.View, entityKey, "#EEEEEE", i);
            }
        }

        /// <summary>
        /// 设置表格背景色
        /// </summary>
        /// <param name="view">当前动态表单的View层接口</param>
        /// <param name="entityKey">单据体的标识</param>
        /// <param name="color">颜色代码</param>
        /// <param name="rowIndex">要设置颜色的数据行的行索引</param>
        private static void GridSetRowBackcolor(IDynamicFormView view, string entityKey, string color, string fieldKey, int rowIndex)
        {
            EntryGrid grid = view.GetControl<EntryGrid>(entityKey);
            grid.SetBackcolor(fieldKey, color, rowIndex);
        }

        /// <summary>
        /// 设置表格背景色
        /// </summary>
        /// <param name="view">当前动态表单的View层接口</param>
        /// <param name="entityKey">单据体的标识</param>
        /// <param name="color">颜色代码</param>
        /// <param name="rowIndex">要设置颜色的数据行的行索引</param>
        private static void GridSetRowBackcolor(IDynamicFormView view, string entityKey, string color, int rowIndex)
        {
            var value = new JSONObject();
            value[rowIndex.ToString()] = color;
            EntryGrid grid = view.GetControl<EntryGrid>(entityKey);
            //grid.SetBackcolor("FPrice", "color", rowIndex);
            var jo = new JSONObject();
            jo["backcolor"] = value;
            var formState = view.GetService<IDynamicFormState>();
            formState.AftInvokeControlMethod(grid.ControlAppearance, "UpdateFieldStates", jo);
        }



    }
}
